<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController; 

class ProductController extends HomeBaseController {
	
	
	public $cates;
	function _initialize() {
		parent::_initialize();
	
	//	print_r(C('URL_ROUTE_RULES'));
	$this->cates=F("Cates");
	$this->assign("cates",$this->cates);
	cookie("jump_self",get_now_url());
	$this->db=D("Product");
	$this->plist=D("ProductList");


		$this->assign ( "nav", "product" );

	}
	


//    public function index(){
//
//
//		if($_POST){
//
//			$order_array=array("top"=>"DESC","listorder" => "DESC","updatetime"=>"DESC","id"=>"DESC");
//
//
//
//			if(I("get.keyword")){
//
//				$map['title']=array('like','%'.I("get.keyword").'%');
//
//			}
//
//
//			$sort=I("get.sort")?I("get.sort"):desc;
//
//			switch(I("get.order")){
//
//				case "zonghe":
//					$order_array="top desc,listorder desc,updatetime desc";
//					break;
//
//				case 'new':
//					$order_array="new desc";
//					break;
//				case 'price':
//
//
//						$order_array=array("price"=>$sort);
//
//					break;
//				default:
//					$order_array="top desc,listorder desc";
//					break;
//
//
//
//			}
//
//
//
//
//			if(I("get.order")){
//
//				$map['title']=array('like','%'.I("get.keyword").'%');
//
//			}
//
//
//
//
//
//			if(I("get.fengge")){
//
//				$map['fengge']=I("get.fengge");
//
//			}
//
//			if(I("get.zuhe")){
//
//				$map['zuhe']=I("get.zuhe");
//
//			}
//			if(I("get.chicun_cate")){
//
//				$map['chicun_cate']=I("get.chicun_cate");
//
//			}
//
//
//			if(I("get.j_id")){
//
//				$ids=D("Qrcode")->where(array("j_id"=>I("get.j_id"),"product_id"=>array("neq","")))->getField("product_id",true);
//
//				$map['id']=array("in",$ids);
//
//			}
//
//
//
//			$map ['status'] = 1;
//
//			$p=I("post.page");
//
//
//
//
//
//
//
//			$count=$this->db->where($map)->count();
//
//
//			$page = $this->page($count,10,$p);
//			$list=$this->db->where($map)->field('content',true)->limit($page->firstRow . ',' . $page->listRows)->order ( $order_array)->select();
//
//
//
//			foreach($list as $k=>$v){
//
//				$s=explode(",",$v["chicun"]);
//				$list[$k]["chicun"]=F("Var_chicun")[$s[0]]["name"];
//
//			}
//
//
//
//
//
//			$this->ajaxReturn($list);
//
//		}
//		else{
//
//
//
//
//
//			$this->assign ( "nav", "product" );
//
//
//			$this->assign ( "seo_title", "全部商品--".C ( "SITE_NAME" ) );
//
//			$this->assign ( "seo_keywords", "全部商品--".C ( "SITE_KEYWORDS" ) );
//			$this->assign ( "seo_description", "全部商品--".C ( "SITE_DESCRIPTION" ) );
//
//			$this->assign ( "timeLineLink", get_now_url() );
//			$this->assign ( "tTitle","全部商品--".C ( "SITE_NAME" ) );
//
//
//			$this->display();
//		}
//
//
//
//
//
//
//
//
//
//	}






//    public  function  search(){
//    	$key=I("get.key");
//
//    	$map['title']=array("like","%".$key."%");
//
//		$map['p_id']=$key;
//		$map['_logic'] = 'or';
//
//
//    	$count=$this->db->where($map)->count();
//    	$page = $this->page($count,20);
//    	$where[where]=$map;
//    	$where[limit]=$page->firstRow;
//    	$where[num]= $page->listRows;
//    	//$list=M("Article")->field('content',true)->where($map)->limit($page->firstRow . ',' . $page->listRows)->order ( $order_array)->select();
//    	$list=$this->db->get_list($where);
//
//    	foreach($list as $k=>$v){
//
//    		$list[$k][title]=str_replace($key,"<span class='red'>".$key."</span>",$list[$k][title]);
//
//
//    	}
//
//
//    	$this->assign("Page",$page->show(""));
//
//
//    	$this->assign("_list",$list);
//
//    	$this->assign("seo_title",C("SITE_NAME"));
//    	$this->display();
//
//
//    }
    
    public function show(){
    	
    	$id=I("get.id");




		$pingjias=D("ViewPingjia")->where(array("product_id"=>$id))->order("id desc")->select();
		$this->assign("pingjias",$pingjias);




    	$data=$this->db->get_one($id);









		$tonglei=D("Product")->where(array("guige_cat"=>$data["guige_cat"]))->field("id,guige")->select();








		$this->assign("_data",$data);
		$this->assign("tonglei",$tonglei);



		$this->assign ( "imgUrl", C ( "SITE_URL" ).$data[thumb] );
		$this->assign ( "timeLineLink", U("show",array('id'=>$id,"j_id"=>$this->u["id"])));
		$this->assign ( "tTitle", $data[title] );
		$this->assign ( "tContent", $data[desc] );



    	$this->assign("seo_title",$data[title]." -- ".C("SITE_NAME"));
    	$this->assign("seo_keywords",$data[tags]?$data[tags]:$data[title]);
    	$this->assign("seo_description",$data[desc]?$data[desc]:$data[name]);






    	$this->display();

    }
    
    
    public function addcart() {
    	$id = $_REQUEST [id];


		$p=D("Product")->where(array("id"=>$id))->find();


		if(!$p["status"]){

			$this->error ( "该商品已经下架" );

		}

		if(!$p["kucun"]){

			$this->error ( "该商品已经有人预定" );

		}


    	if (!$p)
    		$this->error ( "数据不存在" );

    	$cart_list = unserialize ( cookie ( "product_cart" ) );

    	if ($cart_list [$id]) {

    		$cart_list [$id] [num] ++;
    	} else {

    		$cart_list [$id] = array (

    				"id" => $id,
    				"num" => 1
    		)
    		;
    	}


    	cookie ( "product_cart", serialize ( $cart_list ) ,86400);


		if(I('get.jump')){
			$this->success ( "操作成功,前往购买！",U('Home/Cart/index') );
		}else{
			$this->success ( "加入成功！" );
		}

    }



	//前台点击配置更新价格和图片

	public  function get_cs(){


		$id=I("get.id");
		$waikuang=I("post.waikuang");
		$chicun=I("post.chicun");




		$waikuangs=$this->plist->where(array("product_id"=>$id))->group("waikuang")->getField("waikuang",true);


		$chicuns=$this->plist->where(array("product_id"=>$id))->group("chicun")->getField("chicun",true);




		if($waikuang&&$chicun){


			$map_cslist["waikuang"]=$waikuang;
			$map_cslist["chicun"]=$chicun;

			$map_cslist["product_id"]=$id;



			$cs=$this->plist->where($map_cslist)->find();

			$result["cs_id"]=$cs["id"];
			$result["price"]=$cs["price"];
			$result["price_huiyuan"]=$cs["price_huiyuan"];
			$result["thumb"]=$cs["thumb"];

		}






		foreach($chicuns as $k=>$v){

			$map_chicun["chicun"]=$v;

			if($waikuang){
				$map_chicun["waikuang"]=$waikuang;
			}


			$map_chicun["product_id"]=$id;
			$count=$this->plist->where($map_chicun)->count();

			if(!$count){
				$result[cs][$v]=0;
			}	else{
				$result[cs][$v]=1;
			}

		}



		foreach($waikuangs as $k=>$v){

			if($chicun){
				$map_waikuang["chicun"]=$chicun;
			}



			$map_waikuang["waikuang"]=$v;





			$map_waikuang["product_id"]=$id;



			$count=$this->plist->where($map_waikuang)->count();



			if(!$count){
				$result[cs][$v]=0;
			}	else{
				$result[cs][$v]=1;
			}


		}







		$this->ajaxReturn($result);







	}
    
    
    


    
    

    

    

}